/*
 * TableDemo.java requires no other files.
 */

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

/**
 * Title:  MemoryWatch.java
 * Description:  Construct the memory watch flame
 * Copyright:    Copyright (c) 2010
 * @author WANG Jiechao and the Java Tutorial
 * @version 1.0
 */
@SuppressWarnings("unused")
public class MemoryWatch extends JPanel {
    private boolean DEBUG = false;
    JLabel label1;
    JPanel south=new JPanel();

    public MemoryWatch() {
    	super(new BorderLayout());

        JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));

        south.setLayout(new FlowLayout());
        
        label1 = new JLabel("Base address");
        //Set the position of the text, relative to the icon:
        label1.setVerticalTextPosition(JLabel.CENTER);
        label1.setHorizontalTextPosition(JLabel.CENTER); 
        south.add(label1);
        
        
        

        this.add(south,BorderLayout.SOUTH);
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        table.setSize(700, 700);
        scrollPane.setSize(1000, 1000);
        this.setSize(2000, 2000);
        
        add(scrollPane,BorderLayout.CENTER);
    }

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; 
        private Object[][] data = new Object[16][16];        
        
        public MyTableModel(){
        	for(int i=0;i<=15;i++){
        		for(int j=0;j<=15;j++){
        			data[i][j]=new Integer(0);
        		}
        	}
        	data[0][0]="0x0000";
        	for(int i=1;i<=15;i++){
        		int j=i*16;
        		String k=Integer.toHexString(j);
        		data[i][0]="0x00"+new String(k);        	    
        	}
        		
        	
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears on screen.
            if (col < 1) {
                return false;
            } else {
                return true;
            }
        }

        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        public void setValueAt(Object value, int row, int col) {
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                                   + " to " + value
                                   + " (an instance of "
                                   + value.getClass() + ")");
            }

            data[row][col] = value;
            fireTableCellUpdated(row, col);

            if (DEBUG) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }

        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();

            for (int i=0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j=0; j < numCols; j++) {
                    System.out.print("  " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("--------------------------");
        }
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Memory watch 1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        MemoryWatch newContentPane = new MemoryWatch();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

